<template>
<div>
  <!-- 顶部面包屑标识与导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/' }">
      <i class="el-icon-s-promotion"></i> 后台首页
    </el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/admin/temp/category/list' }">系统管理</el-breadcrumb-item>
    <el-breadcrumb-item>部门管理</el-breadcrumb-item>
  </el-breadcrumb>

  <el-divider content-position="left"></el-divider>

  <!-- 操作区域 -->
  <div style="margin: 0 0 20px 0;">
    <el-button type="primary" size="small" @click="$router.push('/sys/dept/add-new')">添加部门</el-button>
  </div>

  <!-- 数据表格 -->
  <el-divider content-position="left">类别列表</el-divider>
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
    <el-table-column label="图标" width="70" align="center">
      <template slot-scope="scope">
        <el-avatar :size="30" :src="scope.row.icon" fit="contain"></el-avatar>
      </template>
    </el-table-column>
    <el-table-column prop="name" label="名称" width="200" align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="description" label="介绍" header-align="center"
                     :show-overflow-tooltip="true"></el-table-column>
    <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
    <el-table-column label="操作" width="100" align="center">
      <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" circle size="mini"
                   @click="openEditDialog(scope.row)"></el-button>
        <el-button type="danger" icon="el-icon-delete" circle size="mini"
                   :disabled="scope.row.isParent == 1"
                   @click="openDeleteConfirm(scope.row)"></el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 返回上层 -->
  <div v-if="currentParentId != 0" style="margin-top: 10px; text-align: right;">
    <el-button size="small" @click="goBack()">返回</el-button>
  </div>

  <!-- 分页控件 -->
  <div style="margin: 10px 0; text-align: right;">
    <el-pagination
        @current-change="changePage"
        layout="total, prev, pager, next"
        :hide-on-single-page="true"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="total">
    </el-pagination>
  </div>
  <!-- 修改类别的弹出表单Dialog -->
  <el-dialog title="修改类别" :visible.sync="dialogFormVisible">
    <el-form :model="editForm" :rules="editRules" label-width="130px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="editForm.name"></el-input>
      </el-form-item>
      <el-form-item label="介绍" prop="keywords">
        <el-input v-model="editForm.description"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="editForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <el-input v-model="editForm.icon"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitEditForm()">确 定</el-button>
    </div>
  </el-dialog>
  </div>

</template>

<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      // 当前类别列表的父级类别ID
      currentParentId: 0,
      // 浏览历史，此数据主要用于返回功能
      history: [],
      // 编辑对话框的可见性
      dialogFormVisible: false,
      // 编辑表单
      editForm: {
        id: '',
        name: '',
        description: '',
        sort: '',
        icon: ''
      },
      // 编辑表单的验证规则
      editRules: {
        name: [
          {required: true, message: '请输入类别名称', trigger: 'blur'},
          {min: 2, max: 15, message: '类别名称长度必须在 2 到 15 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入关键词列表', trigger: 'blur'},
          {min: 2, max: 50, message: '关键词列表长度必须在 2 到 50 个字符', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '请输入图标图片的URL', trigger: 'blur'},
          {min: 20, max: 200, message: '图标图片的URL长度必须在 20 到 200 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^([0-9]{1}|[1-9]{1}[0-9]{1})$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    // 返回
    goBack() {
      console.log('goBack, start...');
      for (let i = 0; i < this.history.length; i++) {
        console.log(this.history[i].id + ' ' + this.history[i].name);
      }
      console.log('---------------')

      let parentCategory = this.history[this.history.length - 1];
      this.currentParentId = parentCategory.parentId;
      this.loadTagList();
      this.history.pop();

      console.log('goBack, done.');
      for (let i = 0; i < this.history.length; i++) {
        console.log(this.history[i].id + ' ' + this.history[i].name);
      }
      console.log('---------------')
    },
    // 切换分页
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadTagList();
    },
    // 打开编辑对话框
    openEditDialog(category) {
      let url = 'http://localhost:9090/sys/dept/' + category.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadTagList();
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 提交编辑表单
    submitEditForm() {
      let url = 'http://localhost:9090/sys/dept/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改类别成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadTagList();
        } else if (jsonResult.state == 40400) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadTagList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 打开删除确认框
    openDeleteConfirm(category) {
      let message = '此操作将永久删除【' + category.name + '】类别，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);
      }).catch(() => {
      });
    },
    // 处理删除
    handleDelete(category) {
      let url = 'http://localhost:9090/sys/dept/' + category.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' + category.name + '】类别成功！',
            type: 'success'
          });
          this.loadTagList();
        } else if (jsonResult.state == 40400) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadTagList();
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    loadTagList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      let url = 'http://localhost:9090/sys/dept?page=' + page;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    }
  },
  mounted() {
    this.loadTagList();
  }
}
</script>